<template>
  <a-tabs class="nav-section" v-model:activeKey="activeKey">
    <a-tab-pane key="timeline" tab="时间线" forceRender>
      <a-spin :spinning="loading">
        <div v-if="!endPage" class="flex flex-center">
          <a-button type="default" @click="pageIndex++">加载更多</a-button>
        </div>
        <template v-for="c of daily" :key="c.val">
          <h4>{{ c.val }}</h4>
          <a-image-preview-group>
            <a-space class="section-timeline-wrap" :size="10">
              <div
                v-for="item of c.children"
                :key="item.id"
                class="box-wrap"
                :class="[{'box-video': item.mime_type.startsWith('video')}]">
                <a-progress :stroke-color="{'0%': '#1890ff', '100%': '#87d068'}" v-if="item.progress > 0" :percent="item.progress" size="small"></a-progress>
                <div v-else class="del-dom"><c-icon icon="DeleteOutlined" class="del-icon"></c-icon></div>
                <a-image
                  v-if="item.mime_type.startsWith('image')"
                  :src="`${item.url}?imageView2/0/h/360`"
                  :preview="{src: item.url}"
                >
                  <template #placeholder>
                    <img :src="`${item.url}?imageMogr2/blur/30x50/quality/30`" class="img-p" alt="" srcset="" />
                  </template>
                </a-image>
                <a-image
                  v-else-if="item.mime_type.startsWith('video')"
                  :src="`${item.url}?vframe/jpg/offset/1|imageView2/0/h/360`"
                  :preview="false"
                  @click="handlePreview(item)">
                  <template #placeholder>
                    <img :src="`${item.url}?vframe/jpg/offset/1|imageMogr2/blur/30x50/quality/30`" class="img-p" alt="" srcset="" />
                  </template>
                </a-image>
                <div v-else class="ant-image other-image"><c-icon icon="FileDoneOutlined" class="file-done"></c-icon></div>
                <dd class="has-edited" v-if="edit" @click="del(item)"><c-icon icon="CloseOutlined" class="c-icon"></c-icon></dd>
                <a-dropdown :trigger="['click']">
                  <a class="ant-dropdown-link" href="javascript:;">使用链接</a>
                  <template #overlay>
                    <div class="flex" style="width:320px;padding:3px 5px;background-color:#fff;">
                      <a-textarea v-model:value="item.url" :auto-size="{ minRows: 2, maxRows: 5 }"></a-textarea>
                      <a href="javascript:;" style="color:#1890ff;flex: 0 0 40px;text-align: right;" v-clipboard:copy="item.url" v-clipboard:success="onCopy">复制</a>
                    </div>
                  </template>
                </a-dropdown>
              </div>
            </a-space>
          </a-image-preview-group>
        </template>
      </a-spin>
      <!--
      <h4>2024年02月29日</h4>
      <div class="section-timeline-wrap">
        <a-image :width="120" :height="120" src="https://static.jsvue.cn/watt/42a98226cffc1e178a82859ae6dbe103738da977434e.jpg">
              <template #placeholder>
                <img :src="`https://static.jsvue.cn/watt/42a98226cffc1e178a82859ae6dbe103738da977434e.jpg?imageMogr2/blur/30x50/quality/30`" class="img-p" alt="" srcset="" />
              </template>
            </a-image>
      </div>
      -->
    </a-tab-pane>
    <a-tab-pane key="filetype" tab="文件类型" forceRender>
      <a-spin :spinning="loading">
        <div v-if="!endPage" class="flex flex-center">
          <a-button type="default" @click="pageIndex++">加载更多</a-button>
        </div>
        <template v-for="c of daily" :key="c.val">
          <h4>{{ c.val }}</h4>
          <a-image-preview-group>
            <a-space class="section-timeline-wrap" :size="10">
              <div
                v-for="item of c.children"
                :key="item.id"
                class="box-wrap"
                :class="[{'box-video': item.mime_type.startsWith('video')}]">
                <a-progress :stroke-color="{'0%': '#1890ff', '100%': '#87d068'}" v-if="item.progress > 0" :percent="item.progress" size="small"></a-progress>
                <div v-else class="del-dom"><c-icon icon="DeleteOutlined" class="del-icon"></c-icon></div>
                <a-image
                  v-if="item.mime_type.startsWith('image')"
                  :src="`${item.url}?imageView2/0/h/360`"
                  :preview="{src: item.url}"
                >
                  <template #placeholder>
                    <img :src="`${item.url}?imageMogr2/blur/30x50/quality/30`" class="img-p" alt="" srcset="" />
                  </template>
                </a-image>
                <a-image
                  v-else-if="item.mime_type.startsWith('video')"
                  :src="`${item.url}?vframe/jpg/offset/1|imageView2/0/h/360`"
                  class="ant-video"
                  :preview="false"
                  @click="handlePreview(item)">
                  <template #placeholder>
                    <img :src="`${item.url}?vframe/jpg/offset/1|imageMogr2/blur/30x50/quality/30`" class="img-p" alt="" srcset="" />
                  </template>
                </a-image>
                <div v-else class="ant-image other-image flex flex-center"><c-icon icon="FileDoneOutlined" class="file-done"></c-icon></div>
                <dd class="has-edited" v-if="edit" @click="del(item)"><c-icon icon="CloseOutlined" class="c-icon"></c-icon></dd>
                <a-dropdown :trigger="['click']">
                  <a class="ant-dropdown-link" href="javascript:;">使用链接</a>
                  <template #overlay>
                    <div class="flex" style="width:320px;padding:3px;background-color:#fff;">
                      <a-textarea v-model:value="item.url" :auto-size="{ minRows: 2, maxRows: 5 }"></a-textarea>
                      <a href="javascript:;" style="color:#1890ff;flex: 0 0 40px;text-align: right;" v-clipboard:copy="item.url" v-clipboard:success="onCopy">复制</a>
                    </div>
                  </template>
                </a-dropdown>
              </div>
            </a-space>
          </a-image-preview-group>
        </template>
      </a-spin>
    </a-tab-pane>

    <template #leftExtra>
      <span>共 <b>{{ files }}</b> 个文件</span>
    </template>
  </a-tabs>
</template>
<script setup>
import { ref, watch, toRefs } from 'vue'
import dayjs from 'dayjs'

import { message } from 'ant-design-vue'
import { StaticURL } from '../api/config'
import { getUploadPage, deleteUpload } from '../api/upload'

const props = defineProps({
  edit: Boolean
})

const { edit } = toRefs(props)
const loading = ref(false)
const pageIndex = ref(1)
const pageSize = ref(30)
const activeKey = ref('timeline')
const daily = ref([])
const files = ref(0)
const endPage = ref(false)
let listData = []

const getData = (first) => {
  if (!pageIndex || !pageIndex.value) return
  loading.value = true
  getUploadPage.send({ pageIndex: pageIndex.value, pageSize: pageSize.value }).then((res = {}) => {
    const { list, total } = res
    if (!list || !list.length) {
      endPage.value = true
      return
    }
    listData = listData.concat(list)
    files.value = total
    if (activeKey.value === 'timeline') { // 时间线
      listData.forEach(ret => {
        ret.url = StaticURL + ret.key
        ret.progress = (1000 - Math.ceil(dayjs().diff(dayjs(ret.createdAt)) / (30 * 86400))) / 10
        const val = dayjs(ret.updatedAt).format('YYYY-MM-DD')
        const index = daily.value.findIndex(met => met.val === val)
        if (first) {
          daily.value = [{ val, children: [ret] }]
          first = false
        } else if (index < 0) daily.value.unshift({ val, children: [ret] })
        else daily.value[index].children.unshift(ret)
      })
    } else {
      listData.forEach(ret => {
        ret.url = StaticURL + ret.key
        ret.progress = (1000 - Math.ceil(dayjs().diff(dayjs(ret.createdAt)) / (30 * 86400))) / 10
        const val = ret.mime_type
        const index = daily.value.findIndex(met => met.val === val)
        if (first) {
          daily.value = [{ val, children: [ret] }]
          first = false
        } else if (index < 0) daily.value.unshift({ val, children: [ret] })
        else daily.value[index].children.unshift(ret)
      })
      console.log(daily.value)
    }
  }).finally(() => {
    loading.value = false
  })
}

const del = (item = {}) => {
  const { id, key } = item
  if (!id || !key) return
  deleteUpload.send({ id, key }).then(() => {
    setTimeout(() => {
      init()
    }, 2000)
  })
}

const init = () => {
  listData = []
  if (pageIndex.value > 1) pageIndex.value = 1
  else getData(1)
}

const handlePreview = (item = {}) => {
  const { url } = item
  window.open(url)
}

const onCopy = () => {
  message.success('您已成功复制使用链接！')
}

watch(activeKey, init)
watch(pageIndex, getData, {
  immediate: true
})
</script>
<style lang="less">
.home-empty {
  height: 100%;
}
.body {
  padding: 15px 0;
}
.nav-section {
  height: 100%;
  .ant-tabs-content-holder {
    overflow-y: auto;
  }
  h4 {
    font-weight: bold;
  }
  .ant-tabs-nav-wrap {
    justify-content: flex-end;
  }
  .section-timeline-wrap {
    margin-bottom: 20px;
    flex-wrap: wrap;
    .ant-image {
      width: 120px;
      height: 120px;
    }
    .ant-space-item .box-wrap {
      display: flex;
      flex-direction: column;
      position: relative;
      .has-edited {
        display: inline-flex;
        position: absolute;
        z-index: 3;
        margin: 0;
        cursor: pointer;
        width: 15px;
        height: 15px;
        border-radius: 10px;
        right: -8px;
        top: -8px;
        background-color: #ff3344;
        border: solid 1px #fff;
        justify-content: center;
        align-items: center;
        .c-icon {
          color: #fff;
          font-size: 12px;
          transform: scale(0.6);
        }
      }
    }
    .ant-space-item .box-video .ant-image {
      &::after {
        content: "";
        display: block;
        width: 45%;
        height: 0;
        padding-bottom: 45%;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        background-image: url(//static.jsvue.cn/5600gg/common/images/playbtn.png);
        background-size: cover;
        cursor: pointer;
      }
    }
    .del-dom {
      position: absolute;
      z-index: 3;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      background-color: rgba(0,0,0,0.5);
      display: flex;
      justify-content: center;
      align-items: center;
      color: #d6d6d6;
      font-size: 16px;
      cursor: not-allowed;
      .del-icon {
        font-size: 48px;
      }
    }
    .img-p {
      width: 100%;
    }
    .img-p, .ant-image-img {
      height: 100%;
      object-fit: cover;
    }
    .del-dom {
      position: absolute;
    }
    .other-image {
      display: inline-flex;
      justify-content: center;
      align-items: center;
      border: solid 1px rgba(232,232,232,0.5);
      border-radius: 5px;
      background-color: #f8f8f8;
      .file-done {
        font-size: 34px;
      }
    }
    .ant-progress-line {
      width: 112px;
    }
  }
}

.ant-image-preview-operations {
  bottom: 0;
  top: auto !important;
}

@media screen and (max-width: 735px) {
  .nav-section .section-timeline-wrap .ant-image {
    width: 74px;
    height: 74px;
  }

  .nav-section .section-timeline-wrap .ant-progress-line {
    width: 68px;
  }

  .nav-section .section-timeline-wrap .other-image {
    .file-done {
      font-size: 26px;
    }
  }

  .nav-section .section-timeline-wrap .del-dom {
    .del-icon {
      font-size: 34px;
    }
  }
}
</style>
